﻿using ScottPlot.Plottables;
using ScottPlot;
using ScottPlot.WinForms;
using System.Drawing;

namespace DetectScars.common
{
    internal class DraggableAxisLines
    {
        FormsPlot formsPlot;
        AxisLine? PlottableBeingDragged = null;
        LinePlot lp = null;
        string type;
        NumericUpDown numericUpDown1 = null;
        NumericUpDown numericUpDown3 = null;
        NumericUpDown numericUpDown4 = null;
        NumericUpDown numericUpDown6 = null;
        NumericUpDown numericUpDown7 = null;
        NumericUpDown numericUpDown9 = null;

        internal void setFormsPlot(FormsPlot formsPlot2, string type_)
        {
            type = type_;
            formsPlot = formsPlot2;
            formsPlot2.MouseDown += FormsPlot2_MouseDown;
            formsPlot2.MouseUp += FormsPlot2_MouseUp;
            formsPlot2.MouseMove += FormsPlot2_MouseMove;
        }

        internal void initGate(NumericUpDown num1, NumericUpDown num3, NumericUpDown num4,
            NumericUpDown num6, NumericUpDown num7, NumericUpDown num9)
        {
            numericUpDown1 = num1;
            numericUpDown3 = num3;
            numericUpDown4 = num4;
            numericUpDown6 = num6;
            numericUpDown7 = num7;
            numericUpDown9 = num9;

        }

        private void FormsPlot2_MouseMove(object? sender, MouseEventArgs e)
        {
            CoordinateRect rect = formsPlot.Plot.GetCoordinateRect(e.X, e.Y, radius: 10);

            if (PlottableBeingDragged is null && lp is null)//PlottableBeingDragged is null || 
            {
                // set cursor based on what's beneath the plottable
                var lineUnderMouse = GetLineUnderMouse(e.X, e.Y);
                var lpm = GetLineUnderMouse2(e.X, e.Y);
                if (lineUnderMouse is null && lpm is null)
                {
                    formsPlot.Cursor = Cursors.Default;
                }
                else if (lpm is null && lineUnderMouse.IsDraggable && lineUnderMouse is VerticalLine) formsPlot.Cursor = Cursors.SizeWE;
                else if (lpm is null && lineUnderMouse.IsDraggable && lineUnderMouse is HorizontalLine) formsPlot.Cursor = Cursors.SizeNS;
                else if (lpm is not null) formsPlot.Cursor = Cursors.SizeNS;
            }
            else
            {
                // update the position of the plottable being dragged
                if (PlottableBeingDragged is HorizontalLine hl)
                {
                    hl.Y = rect.VerticalCenter;
                    hl.Text = rect.VerticalCenter.ToString("0.0");
                    if (type == "C")
                    {
                        SocttPlotUtils.SetBAindexH(rect.VerticalCenter);
                    }
                }
                else if (PlottableBeingDragged is VerticalLine vl)
                {
                    vl.X = rect.HorizontalCenter;
                    vl.Text = rect.HorizontalCenter.ToString("0.0");
                    if (type == "C")
                    {
                        SocttPlotUtils.SetBAindexV(rect.HorizontalCenter);
                    }
                }
                else if (lp is LinePlot line)
                {
                    //刷新当前闸门
                    //line.Start.Y = rect.VerticalCenter;
                    double len = (line.End.X - line.Start.X) / 2;
                    double xs = rect.HorizontalCenter - len;
                    double xe = rect.HorizontalCenter + len;
                    double h = rect.VerticalCenter;
                    //line.Line.
                    //line.Line.WithDelta(1,1);
                    SocttPlotUtils.RefreshGates(xs, xe, h, line.Color,0);
                }
                formsPlot.Refresh();
            }
        }

        private void FormsPlot2_MouseUp(object? sender, MouseEventArgs e)
        {
            if(PlottableBeingDragged is not null)
            {
                if (type == "B")
                {
                    SocttPlotUtils.SetBAindex(PlottableBeingDragged.Position);
                }
                PlottableBeingDragged = null;
                if (type != "")
                {
                    formsPlot.UserInputProcessor.Disable(); // enable panning again Enable
                }
                else
                {
                    formsPlot.UserInputProcessor.Enable();
                }
            }
            if (lp is not null)
            {
                var lpm = GetLineUnderMouse2(e.X, e.Y);
                if (lpm is not null)
                {
                    lp = lpm;
                }
                //刷新闸门
                double xs = lp.Start.X;
                double h = lp.Start.Y;
                ScottPlot.Color color = lp.Color;
                if (color == Colors.C0)
                {
                    numericUpDown1.Value = (decimal)xs;
                    numericUpDown3.Value = (decimal)h;
                }
                else if (color == Colors.C1)
                {
                    numericUpDown4.Value = (decimal)xs;
                    numericUpDown6.Value = (decimal)h;
                }
                else if (color == Colors.C2)
                {
                    numericUpDown7.Value = (decimal)xs;
                    numericUpDown9.Value = (decimal)h;
                }
                SocttPlotUtils.RefreshGates(xs, lp.End.X, h, color, 0);
                lp = null;
                formsPlot.UserInputProcessor.Disable();
                SocttPlotUtils.drag_flag = false;
            }
            
            formsPlot.Refresh();
            //刷新a扫
            
        }

        private void FormsPlot2_MouseDown(object? sender, MouseEventArgs e)
        {
            var lineUnderMouse = GetLineUnderMouse(e.X, e.Y);
            if (lineUnderMouse is not null)
            {
                PlottableBeingDragged = lineUnderMouse;
                if (type != "")
                {
                    formsPlot.UserInputProcessor.Enable(); // enable panning again Enable
                }
                else
                {
                    formsPlot.UserInputProcessor.Disable(); // disable panning while dragging Disable
                }
                
            }
            var lpm = GetLineUnderMouse2(e.X, e.Y);
            if (lpm is not null)
            {
                lp = lpm;
                formsPlot.UserInputProcessor.Disable();
                SocttPlotUtils.drag_flag = true;
            }
        }

        internal AxisLine? GetLineUnderMouse(float x, float y)
        {
            CoordinateRect rect = formsPlot.Plot.GetCoordinateRect(x, y, radius: 10);

            foreach (AxisLine axLine in formsPlot.Plot.GetPlottables<AxisLine>().Reverse())
            {
                if (axLine.IsUnderMouse(rect))
                    return axLine;
            }
            return null;
        }
        internal LinePlot? GetLineUnderMouse2(float x, float y)
        {
            CoordinateRect rect = formsPlot.Plot.GetCoordinateRect(x, y, radius: 10);

            foreach (LinePlot axLine in formsPlot.Plot.GetPlottables<LinePlot>().Reverse())
            {
                if (rect .HorizontalCenter > axLine.Start.X&& rect.HorizontalCenter < axLine.End.X&&Math.Abs(rect.VerticalCenter - axLine.Start.Y)<1)
                {
                    return axLine;
                }
                
            }
            return null;
        }

        //非鼠标移动
        internal static void FormsPlot_Move(FormsPlot formsPlot,string type,double s)
        {
            if (type == "H")
            {
                foreach (AxisLine axLine in formsPlot.Plot.GetPlottables<HorizontalLine>().Reverse())
                {
                    axLine.Position += s;
                    axLine.Text = axLine.Position.ToString("0.0");
                    SocttPlotUtils.SetBAindexH(axLine.Position);
                }
            }
            else if (type == "V")
            {
                foreach (AxisLine axLine in formsPlot.Plot.GetPlottables<VerticalLine>().Reverse())
                {
                    axLine.Position += s;
                    axLine.Text = axLine.Position.ToString("0.0");
                    SocttPlotUtils.SetBAindexV(axLine.Position);

                }
            }
            formsPlot.Refresh();



        }

    }
}
